package com.factors.HuangHePing.SchoolTypeCount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class SchoolTypeScoreMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    private static final String C = "0-59";
    private static final String B = "60-79";
    private static final String A = "80-100";

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        String[] columns = value.toString().split(",");
        if (columns.length >= 20) {
            // 直接解析学校类型和考试分数，假设输入数据格式正确
            String schoolType = columns[12];
            float examScore = Float.parseFloat(columns[19]);

            // Determine the score range
            String scoreRange;
            if (examScore >= 0 && examScore <= 59) {
                scoreRange = C;
            } else if (examScore >= 60 && examScore <= 79) {
                scoreRange = B;
            } else if (examScore >= 80 && examScore <= 100) {
                scoreRange = A;
            } else {
                // 如果考试分数不在预期范围内，则跳过该记录
                return;
            }
            //将学校类型和分数区间通过冒号连接起来
            context.write(new Text(schoolType + ":" + scoreRange), new IntWritable(1));//创建了一个新的 IntWritable 对象
                                                                                                   // 值 1 表示每条记录对应一个单位的计数，
                                                                                                   //Reducer 可以通过累加这些 1 来计算每个复合键出现的次数
        }
    }
}